<template>
    <el-form label-width="auto">
        <el-form-item label="模型">
            {{ modelname }}
        </el-form-item>
        <el-form-item label="尺寸">
            {{ modelWidth }} * {{ modelHeight }} * {{ modelDepth }}
        </el-form-item>
        <el-form-item label="备注">
            {{ modelcomment }}
        </el-form-item>
        <!-- 收集模型内部的虚拟五金 -->
        <el-form-item label="虚拟五金">
            <ul>
                <li v-for="item in hws">
                    {{ item.name }} {{ item.qty }}
                </li>
            </ul>
        </el-form-item>
        <!-- 组件的高级变量 -->
        <el-form-item label="变量">
            <ul>
                <li v-for="item in parameters">
                    {{ item.displayName }} {{ item.value }}
                </li>
            </ul>
        </el-form-item>
        <!-- 收集模型内部的颜色 -->
    </el-form>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref, watch, unref } from 'vue';
import { useCurrentModelStore } from '@/stores/current';
import { KujialeParamModel, type KujialeParameter } from '@/core3d/types/KujialeJsonModels/KujialeParamModel';
import { AssemblyProductGroup, type Base3DGroup } from '@/core3d/types/DrawingGroups/groups';
import { TraverseAndFilter } from '@/core3d/helpers/GroupTraverseHelper';
import type { AssemblyProductModel } from '@/core3d/types/Models';

interface HWItem {
    name: string;
    qty: number;
}

export default defineComponent({
    setup(props, { emit, attrs }) {
        const modelname = ref("");
        const modelcomment = ref("");
        const modelWidth = ref(0);
        const modelHeight = ref(0);
        const modelDepth = ref(0);
        const hws = ref<HWItem[]>([]);
        const parameters = ref<KujialeParameter[]>([]);

        const store = useCurrentModelStore();
        watch(() => store.currentCurrentModelData, (n, o) => {
            if (n) {
                parameters.value.length = 0;

                const groups = store.currentCurrentModelData as Base3DGroup[];
                if (groups.length == 1) {
                    //重置
                    hws.value.length = 0;


                    const [group] = groups;
                    const { model } = group;
                    const { kujialeModel: kjlmodels } = model;
                    const [kjlmodel] = kjlmodels as KujialeParamModel[];

                    if (group instanceof AssemblyProductGroup) {
                        const agroupmodel = group.model as AssemblyProductModel;
                        modelname.value = agroupmodel.kujialeAssemblyModel.name;
                        modelWidth.value = agroupmodel.kujialeAssemblyModel.size.x;
                        modelHeight.value = agroupmodel.kujialeAssemblyModel.size.z;
                        modelDepth.value = agroupmodel.kujialeAssemblyModel.size.y;
                    }
                    else {
                        modelname.value = kjlmodel.modelName;
                        modelWidth.value = kjlmodel.getModelWidth();
                        modelHeight.value = kjlmodel.getModelHeight();
                        modelDepth.value = kjlmodel.getModelDepth();
                        modelcomment.value = "空";//todo 不知道是哪个字段
                        //读取变量
                        parameters.value = kjlmodel.parameters
                    }

                } else {
                    modelname.value = "";//不为1时，说明没有选中，就是整个json的信息
                }

                //读取虚拟五金
                const hwgroups = TraverseAndFilter(
                    store.currentCurrentModelData as Base3DGroup[],
                    (m) => m.modelTypeId == 4,
                    (cm) => false,
                    (cg) => { },
                    (cg, cm) => {
                    });
                const hwdict: { [key: string]: number } = {};
                hwgroups.map(g => {
                    const [kjlmodel] = g.model.kujialeModel!;
                    const pa = kjlmodel.parameters.find(it => it.name == 'count');//数量变量
                    return {
                        name: kjlmodel.modelName,
                        qty: pa === undefined ? 1 : parseFloat(pa.value.toString())
                    }
                }).forEach(p => {
                    if (p.name in hwdict) {
                        hwdict[p.name] += p.qty;
                    } else {
                        hwdict[p.name] = p.qty;
                    }
                });

                for (var key in hwdict) {
                    hws.value.push({
                        name: key,
                        qty: hwdict[key]
                    })
                }
            }
        });

        return { modelname, modelWidth, modelHeight, modelDepth, modelcomment, hws, parameters };
    }
});
</script>